<script setup>
import {listPoiComment,deletePoiComment,updatePoiComment} from "@/api/comment.js"
import { ref } from 'vue'
import {Delete, Edit, Plus} from "@element-plus/icons-vue";
import {ElMessage, ElMessageBox} from "element-plus";
//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(5)//每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
  pageSize.value = size
  getListComment()
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num
  getListComment()
}
const parentBorder = ref(false)
const commentData = ref()
const username = ref('')
//控制抽屉是否显示
const visibleDrawer = ref()
const title = ref('')
const commentModel = ref({
  commentId: "",
  username:"",
  userId: "",
  poiName: "",
  poiId: "",
  commentContent: "",
  createTime: "",
  updateTime: "",
})
//展示抽屉
const showDialog = (row) => {
  visibleDrawer.value = true;
  title.value = '编辑';
  commentModel.value.commentId = row.commentId;
  commentModel.value.userId = row.userId;
  commentModel.value.username = row.username;
  commentModel.value.poiName = row.poiName;
  commentModel.value.poiId = row.poiId;
  commentModel.value.commentContent = row.commentContent;
  commentModel.value.createTime = row.createTime;
  commentModel.value.updateTime = row.updateTime;
}
//清空模型的数据
const clearData = () => {
  commentModel.value = {
    commentId: "",
    username:"",
    userId: "",
    poiId: "",
    poiName: "",
    commentContent: "",
    createTime: "",
    updateTime: "",
  }
};
const getListComment = async () => {
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    username: username.value?username.value:null
  }
  let result = await listPoiComment(params);
  //渲染视图
  total.value = result.data.total;
  commentData.value = result.data.items;
}
getListComment()

const updateComment = async () => {
  let result = await updatePoiComment(commentModel.value);
  ElMessage.success(result.msg? result.msg:'修改成功');
  visibleDrawer.value = false;
  await getListComment();
}
const deleteComment = async (row) => {
  //提示用户 确认框
  ElMessageBox.confirm(
      '你确认要删除该评论吗?',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        //调用接口
        let result = await deletePoiComment(row.id);
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        //刷新列表
        await getListComment();
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '用户取消了删除',
        })
      })
}
</script>
<template>
  <el-card>
    <template #header>
      <div class="clearfix" style="display: flex;justify-content: space-between">
        <span>评论管理</span>
        <span> border: <el-switch v-model="parentBorder" /></span>
      </div>
    </template>

    <el-form inline>
      <el-form-item label="用户：">
        <el-input placeholder="请输入" v-model="username" style="width: 100px">
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getListArticleComment()">搜索</el-button>
        <el-button @click="username='';getListArticleComment()">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="commentData" :border="parentBorder" style="width: 100%">
      <el-table-column label="时间" prop="createTime" />
      <el-table-column label="地点" prop="poiName" />
      <el-table-column label="用户" prop="username" />
      <el-table-column label="内容" prop="commentContent" />
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="deleteComment(row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页条 -->
    <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5 ,10, 15]"
                   layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
                   @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />
    <!-- 抽屉 -->
    <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="30%">
      <!-- 添加文章表单 -->
      <el-form :model="commentModel" label-width="100px" >
        <el-form-item label="文章标题" >
          <el-input v-model="commentModel.poiName" placeholder="请输入标题"></el-input>
        </el-form-item>
        <el-form-item label="用户" >
          <el-input v-model="commentModel.username" placeholder="请输入标题"></el-input>
        </el-form-item>
        <el-form-item label="评论内容" label-width="100px">
          <div class="editor" style="width: 100%">
            <el-input
                type="textarea"
                :rows="1"
                v-model="commentModel.commentContent"
                placeholder="请输入文章内容">
            </el-input>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="updateComment()">更改</el-button>
          <el-button type="info" @click="visibleDrawer=false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </el-card>
</template>

<style scoped>

</style>
